//
//  MKReaderSettingView.swift
//  Fanmugua
//
//  Created by MK on 2021/3/31.
//  Copyright © 2021 taolang. All rights reserved.
//

import UIKit

@objc protocol MKReaderSettingDelegate:NSObjectProtocol {
    ///设置字体
    @objc func settingFontSize(settingView:MKReaderSettingView)
    ///设置背景色
    @objc func settingBgColor(settingView:MKReaderSettingView)
    //设置翻页效果
    @objc func settingPagingEffect(settingView:MKReaderSettingView)
    //自动翻页
    @objc optional func autoPagingAction(settingView:MKReaderSettingView)
}

private let MKReaderSettingViewHeight:CGFloat = 168 + 62

class MKReaderSettingView: UIView {

    @IBOutlet weak var labFontTitle: UILabel!
    @IBOutlet weak var btnFontSub: UIButton!
    @IBOutlet weak var btnFontAdd: UIButton!
    @IBOutlet weak var labFontSize: UILabel!
    
    @IBOutlet weak var fontConst: NSLayoutConstraint!
    @IBOutlet weak var fontView: UIView!
    @objc var isShow:Bool = false ///是否已展示
    @objc var isShortStory:Bool = false ///是否短故事
    @objc weak var delegate:MKReaderSettingDelegate!

    
    @objc class func createView() -> MKReaderSettingView?{
        let view:MKReaderSettingView = Bundle.main.loadNibNamed("MKReaderSettingView", owner: nil, options: nil)?.last as! MKReaderSettingView
        view.frame = CGRect(x: 0,
                            y: SCREEN_HEIGHT,
                            width: SCREEN_WIDTH,
                            height: MKReaderSettingViewHeight)
        return view
    }
    
    @objc func btnFontSubAction(){
        let size = NSNumber(value:MKReaderCfg.shared().fontSize.intValue - MKREADER_FONT_SIZE_SPACE)
        if size.intValue >= MKREADER_FONT_SIZE_MIN {
            labFontSize.text = "\(size.stringValue)"
            saveAllSize(size: size)
            delegate?.settingFontSize(settingView: self);
        }
    }
    @objc func btnFontAddAction(){
        let size = NSNumber(value:MKReaderCfg.shared().fontSize.intValue + MKREADER_FONT_SIZE_SPACE)
        if size.intValue <= MKREADER_FONT_SIZE_MAX{
            labFontSize.text = "\(size.stringValue)"
            saveAllSize(size: size)
            delegate?.settingFontSize(settingView: self);
        }
    }
    func saveAllSize(size : NSNumber){
        
        if size.intValue == 12 {
            MKReaderCfg.shared().characterSpacingSize = NSNumber(value:0);
            MKReaderCfg.shared().lineSpacing = NSNumber(value:18);
            MKReaderCfg.shared().paragraphSpacing = NSNumber(value:18);
        }else if size.intValue == 16{
            
            MKReaderCfg.shared().characterSpacingSize = NSNumber(value:0);
            MKReaderCfg.shared().lineSpacing = NSNumber(value:24);
            MKReaderCfg.shared().paragraphSpacing = NSNumber(value:22);
        }else if size.intValue == 20{
            
            MKReaderCfg.shared().characterSpacingSize = NSNumber(value:0);
            MKReaderCfg.shared().lineSpacing = NSNumber(value:28);
            MKReaderCfg.shared().paragraphSpacing = NSNumber(value:28);
        }else if size.intValue == 24{
            
            MKReaderCfg.shared().characterSpacingSize = NSNumber(value:0);
            MKReaderCfg.shared().lineSpacing = NSNumber(value:34);
            MKReaderCfg.shared().paragraphSpacing = NSNumber(value:30);
        }else if size.intValue == 28{
            
            MKReaderCfg.shared().characterSpacingSize = NSNumber(value:0);
            MKReaderCfg.shared().lineSpacing = NSNumber(value:38);
            MKReaderCfg.shared().paragraphSpacing = NSNumber(value:38);
        }
        
        MKReaderCfg.shared().fontSize = size
        MKReaderCfg.shared().saveConfigure()
    }
    @objc func btnColorAction(_ item:UIButton!){
        if item.tag == MKReaderCfg.shared().bgColorIndex.intValue {
            return
        }
        
        MKReaderCfg.shared().bgColorIndex = NSNumber(value: item.tag)
        MKReaderCfg.shared().saveConfigure()
        
//        btnColor1.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)
//        btnColor2.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)
//        btnColor3.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)
//        btnColor4.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)
//        btnColor5.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)
        
//        if (MKReaderCfg.shared().bgColorIndex == 0){
//            btnColor1.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 1){
//            btnColor2.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 2){
//            btnColor3.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 3){
//            btnColor4.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 4){
//            btnColor5.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }
        delegate?.settingBgColor(settingView: self)
    }
    
    @objc func btnEffectAction(_ item:UIButton!){
        if (item.tag == MKReaderCfg.shared().effectIndex.intValue){
            return
        }
        MKReaderCfg.shared().effectIndex = NSNumber(value: item.tag)
        MKReaderCfg.shared().saveConfigure()
        let effectType = MKReaderEffectType(rawValue: MKReaderCfg.shared().effectIndex.intValue)
//        btnPageStyle1.mk_setBorderWidth(1, andColor: effectType == .scroll ? MKReaderCfg.shared().selectedColor : MKReaderCfg.shared().unSelectedColor)
//        btnPageStyle2.mk_setBorderWidth(1, andColor: effectType == .translation ? MKReaderCfg.shared().selectedColor : MKReaderCfg.shared().unSelectedColor)
//        btnPageStyle3.mk_setBorderWidth(1, andColor: effectType == .simulation ? MKReaderCfg.shared().selectedColor : MKReaderCfg.shared().unSelectedColor)
        delegate?.settingPagingEffect(settingView: self)
    }
    
    @objc func showView(show:Bool){
        if show == isShow{
            return
        }
        UIView.animate(withDuration: MKREADER_ANIMATE_DURATION,
                       delay: 0,
                       options: .curveEaseOut) {[weak self] () in
            var y = show ? SCREEN_HEIGHT - MKReaderSettingViewHeight - MKReaderBottomViewHeight : SCREEN_HEIGHT
            if self!.isShortStory {
                y = show ? SCREEN_HEIGHT - 56 - MKReaderBottomViewHeight : SCREEN_HEIGHT
                
                self?.frame.origin = CGPoint(x: 0, y: y)
            }else{
                
                self?.frame.origin = CGPoint(x: 0, y: y)
            }
            
        } completion: { [weak self] (ok) in
            self?.isShow = show
        }
    }
    
    @objc func refreshStyleUI(){
        
        
        if MKReaderCfg.shared().bgColorIndex == 0{
            
            backgroundColor = .white
            
            fontView.backgroundColor = .white
            
            btnFontAdd.backgroundColor = .hex_F5F5F5()
            
            btnFontSub.backgroundColor = .hex_F5F5F5()
            
            labFontSize.textColor = .hex_333333()
            
            labFontTitle.textColor = .hex_333333()
            
            btnFontAdd.setTitleColor(.hex_333333(), for: .normal)
            
            btnFontSub.setTitleColor(.hex_333333(), for: .normal)
            
        }else{
            
            backgroundColor = .hex_191820()
            
            fontView.backgroundColor = .hex_191820()
            
            labFontSize.textColor = .white
            
            btnFontAdd.backgroundColor = .hex_313538()
            
            btnFontSub.backgroundColor = .hex_313538()
            
            labFontTitle.textColor = .white
            
            btnFontAdd.setTitleColor(.white, for: .normal)
            
            btnFontSub.setTitleColor(.white, for: .normal)
            
        }
        
        
//        labFontTitle.textColor = MKReaderCfg.shared().menuTextColor
        
//        labFontSize.textColor = MKReaderCfg.shared().menuTextColor
        labFontSize.text = MKReaderCfg.shared().fontSize.stringValue
        
//        btnFontSub.setTitleColor(MKReaderCfg.shared().menuTextColor, for: .normal)
//        btnFontSub.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)

//        btnFontAdd.setTitleColor(MKReaderCfg.shared().menuTextColor, for: .normal)
//        btnFontAdd.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().unSelectedColor)
        
//        let effectType = MKReaderEffectType(rawValue: MKReaderCfg.shared().effectIndex.intValue)
//
//        if (MKReaderCfg.shared().bgColorIndex == 0){
////            btnColor1.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 1){
////            btnColor2.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 2){
////            btnColor3.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 3){
////            btnColor4.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }else if (MKReaderCfg.shared().bgColorIndex == 4){
////            btnColor5.mk_setBorderWidth(1, andColor: MKReaderCfg.shared().selectedColor)
//        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
//        btnFontSub.mk_setCornerValue(14)
        btnFontSub.addTarget(self, action: #selector(btnFontSubAction), for: .touchUpInside)
        
//        btnFontAdd.mk_setCornerValue(14)
        btnFontAdd.addTarget(self, action: #selector(btnFontAddAction), for: .touchUpInside)
        
        btnFontSub.layer.masksToBounds = true
        btnFontSub.layer.cornerRadius = 14
        
        btnFontAdd.layer.masksToBounds = true
        btnFontAdd.layer.cornerRadius = 14
        
        refreshStyleUI()
    }
    @IBAction func autoPage(_ sender: Any) {
        
        delegate?.autoPagingAction!(settingView: self)
        
    }
    @objc func statusForShortStory(){
        
        isShortStory = true
        fontConst.constant = 0
        self.frame = CGRect(x: 0,
                            y: SCREEN_HEIGHT,
                            width: SCREEN_WIDTH,
                            height: 56)
        self.layoutIfNeeded()
    }
}
